package xyz.xuminghai.javafx_learn.scene.shape;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CubicCurve;
import javafx.stage.Stage;

/**
 * 2023/8/4 20:12 星期五<br/>
 * CubicCurve类定义 (x,y) 坐标空间中的三次贝塞尔参数曲线段。
 * 使用指定点(startX, startY) (controlX1, controlY1)和(controlX2, controlY2) (endX, enfY) ) 相交的曲线。
 *
 * @author xuMingHai
 */
public class CubicCurveApp extends Application {

    private final BorderPane root = new BorderPane();


    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        Circle start = new Circle(10.0);
        start.setFill(Color.RED);
        setMouseDragAction(start);

        Circle control1 = new Circle(10.0);
        control1.setFill(Color.DEEPPINK);
        setMouseDragAction(control1);

        Circle control2 = new Circle(10.0);
        control2.setFill(Color.DARKVIOLET);
        setMouseDragAction(control2);

        Circle end = new Circle(10.0);
        end.setFill(Color.BLUE);
        setMouseDragAction(end);

        CubicCurve cubicCurve = new CubicCurve();
        cubicCurve.setStroke(Color.BLACK);
        cubicCurve.setFill(null);
        cubicCurve.startXProperty().bind(start.layoutXProperty());
        cubicCurve.startYProperty().bind(start.layoutYProperty());
        cubicCurve.controlX1Property().bind(control1.layoutXProperty());
        cubicCurve.controlY1Property().bind(control1.layoutYProperty());
        cubicCurve.controlX2Property().bind(control2.layoutXProperty());
        cubicCurve.controlX2Property().bind(control2.layoutYProperty());
        cubicCurve.endXProperty().bind(end.layoutXProperty());
        cubicCurve.endYProperty().bind(end.layoutYProperty());

        root.setRight(new VBox(20.0, new Label("start", new Circle(start.getRadius(), start.getFill())),
                new Label("control1", new Circle(control1.getRadius(), control1.getFill())),
                new Label("control2", new Circle(control2.getRadius(), control2.getFill())),
                new Label("end", new Circle(end.getRadius(), end.getFill()))));
        Pane pane = new Pane(cubicCurve, start, control1, control2, end);
        pane.setMaxSize(500.0, 400.0);
        BorderPane.setAlignment(pane, Pos.CENTER);
        root.setCenter(pane);
    }

    private void setMouseDragAction(Circle circle) {
        double[] offset = {0.0, 0.0};
        // 保存鼠标按下时相对于源点的坐标（排除半径）
        circle.setOnMousePressed(event -> {
            offset[0] = event.getX();
            offset[1] = event.getY();
        });
        // 计算出具体移动的距离加上原本的坐标
        circle.setOnMouseDragged(event -> circle.relocate(circle.getLayoutX() + (event.getX() - offset[0] - circle.getRadius()),
                circle.getLayoutY() + (event.getY() - offset[1] - circle.getRadius())));
    }


    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}
